package edu.bu.cs673.superkids123.parents.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.AbstractNavigationView;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;
import edu.bu.cs673.superkids123.children.model.resources.ResourceType;
import edu.bu.cs673.superkids123.common.model.Child;
import edu.bu.cs673.superkids123.parents.controller.CustomizationController;

public class CustomizationView extends AbstractNavigationView implements ItemListener, ActionListener {

	private JPanel panel;
	private JPanel imagePanel;
	private JComboBox imageComboBox,childComboBox,audioComboBox;
	private JButton submitBtn;
	ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance();
	
	ArrayList<Child> children = (ArrayList<Child>) getMainFrame().getController(CustomizationController.class).getChildren();
	Child current;
	
	List<String> imageList = getMainFrame().getController(CustomizationController.class).getAvailableImagesList();
	List<String> audioList = getMainFrame().getController(CustomizationController.class).getAvailableAudioList();
	
	private static final int[] COLUMN_WIDTHS = {250, 550};
	private static final int[] ROW_HEIGHTS = {0};
	
	
	public CustomizationView(AbstractFrame mainFrame) {
		super(mainFrame);
	}

	@Override
	public String getTitle() {
		return "Customize Resources";
	}

	@Override
	public String getBorderTitle() {
		return null;
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	protected JComponent layout() {
		
		this.setParentsButtonVisible(false);
		this.setAvatarVisible(false);
		
		children = (ArrayList<Child>) getMainFrame().getController(CustomizationController.class).getChildren();
		current = children.size() > 0 ? children.get(0) : null;
		
		panel = new JPanel();
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = COLUMN_WIDTHS;
		gbl_panel.rowHeights = ROW_HEIGHTS;
		panel.setLayout(gbl_panel);
		
		GridBagConstraints gbc = new GridBagConstraints();

		
		//label
		JLabel childLabel = new JLabel("Child: ");
		gbc.gridx = 0;
		gbc.gridy = 2;
		panel.add(childLabel, gbc);
		
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weighty = 1.0;
		gbc.gridx = 0;
		gbc.gridy = 3;
		
		childComboBox = new JComboBox(children.toArray());
		childComboBox.setSelectedItem(current.getName());
		childComboBox.addItemListener(this);
		
		panel.add(childComboBox, gbc);
		
		// Image Selector Panel
		JPanel selector = imageSelector();
		GridBagConstraints gbcSelector = new GridBagConstraints();
		gbcSelector.fill = GridBagConstraints.BOTH;
//		gbcSelector.weighty = 1.0;
		gbcSelector.gridx = 1;
		gbcSelector.gridy = 0;
		
		panel.add(selector, gbcSelector);
		
		return panel;
	}
	
	private JPanel imageSelector() {
		
		children = (ArrayList<Child>) getMainFrame().getController(CustomizationController.class).getChildren();
		current = children.size() > 0 ? children.get(0) : null;
		
		JPanel p = new JPanel();
		
		
		GridBagLayout gbl = new GridBagLayout();
		gbl.columnWidths = new int[] {50, 0};
		gbl.rowHeights = new int[] {50, 0};
		p.setLayout(gbl);
		
		GridBagConstraints gbc = new GridBagConstraints();
		
		// label
		JLabel themeLabel = new JLabel("Theme: ");
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		p.add(themeLabel, gbc);
		
		// ComboBox - Image Resources
		
		imageComboBox = new JComboBox(imageList.toArray());
		String currentChild =current.getImageResource().toString();
		currentChild=currentChild.replace("/strings/","");
		currentChild=currentChild.replace(".xml", "");
		currentChild=currentChild.replace("_num", "");
		currentChild=currentChild.trim();
		String firstLetter=currentChild.charAt(0)+"";
		firstLetter=firstLetter.toUpperCase();
		currentChild=currentChild.substring(1);
		String aux=firstLetter.concat(currentChild);
		int currentIndex=imageList.indexOf(aux);
		imageComboBox.setSelectedIndex(currentIndex);
		
		imageComboBox.addItemListener(this);
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		p.add(imageComboBox, gbc);
		
		// label
		JLabel audioLabel = new JLabel("Audio: ");
		gbc.gridx = 3;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		p.add(audioLabel, gbc);
		
		// ComboBox - Audio Resources
		
		audioComboBox = new JComboBox(audioList.toArray());
		String currentAudio=current.getAudioResource().toString();
		currentAudio=currentAudio.replace("/strings/","");
		currentAudio=currentAudio.replace(".xml", "");
		currentAudio=currentAudio.trim();
		String firstLetter2=currentAudio.charAt(0)+"";
		firstLetter2=firstLetter2.toUpperCase();
		currentAudio=currentAudio.substring(1);
		String aux2=firstLetter2.concat(currentAudio);
		int currentIndex2=audioList.indexOf(aux2);
		audioComboBox.setSelectedIndex(currentIndex2);
		audioComboBox.addItemListener(this);
		gbc.gridx = 4;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		p.add(audioComboBox, gbc);
		
		// Images
		imagePanel = new JPanel(new GridLayout(2, 3));
		updateImagePreview(imageComboBox.getSelectedItem().toString());
		
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		
		p.add(imagePanel, gbc);
		
		submitBtn = new JButton("Submit");
		submitBtn.addActionListener(this);
		
		submitBtn.setEnabled(true);
		GridBagConstraints gbc_btnDelete = new GridBagConstraints();
		gbc_btnDelete.insets = new Insets(0, 0, 5, 5);
		gbc_btnDelete.gridx = 5;
		gbc_btnDelete.gridy = 3;
		p.add(submitBtn, gbc_btnDelete);
		submitBtn.addActionListener(this);
		
		
		return p;
	}
	
	private void updateImagePreview(String type) {

		imagePanel.removeAll();
		
		ResourceType resourceType = ResourceType.valueOf(type);
		ImageIcon[] images = getMainFrame().getController(CustomizationController.class).getSampleImages(resourceType);
			
		for (int i = 0; i < images.length; i++) {
			JButton button = new JButton();
			button.setName(String.format("%s", i));
			button.setIcon(images[i]);
			button.addActionListener(this);
			imagePanel.add(button);
		}
		
		imagePanel.revalidate();
		imagePanel.repaint();
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		
		if(e.getSource() == childComboBox){
			int currentChildIndex=childComboBox.getSelectedIndex();
			current = children.size() > 0 ? children.get(currentChildIndex) : null;
			String currentChild =current.getImageResource().toString();
			currentChild=currentChild.replace("/strings/","");
			currentChild=currentChild.replace(".xml", "");
			currentChild=currentChild.replace("_num", "");
			currentChild=currentChild.trim();
			String firstLetter=currentChild.charAt(0)+"";
			firstLetter=firstLetter.toUpperCase();
			currentChild=currentChild.substring(1);
			String aux=firstLetter.concat(currentChild);
			int currentIndex=imageList.indexOf(aux);
			imageComboBox.setSelectedIndex(currentIndex);
			
			
			String currentAudio=current.getAudioResource().toString();
			currentAudio=currentAudio.replace("/strings/","");
			currentAudio=currentAudio.replace(".xml", "");
			currentAudio=currentAudio.trim();
			String firstLetter2=currentAudio.charAt(0)+"";
			firstLetter2=firstLetter2.toUpperCase();
			currentAudio=currentAudio.substring(1);
			String aux2=firstLetter2.concat(currentAudio);
			int currentIndex2=audioList.indexOf(aux2);
			audioComboBox.setSelectedIndex(currentIndex2);
			
			updateImagePreview(aux);
			
		}
		
		if (e.getSource() == imageComboBox && e.getStateChange() == ItemEvent.SELECTED) {
			imageComboBox.setSelectedItem(current.getImageResource().toString());
			updateImagePreview(imageComboBox.getSelectedItem().toString());
		}
		else if (e.getSource() == audioComboBox && e.getStateChange() == ItemEvent.SELECTED) {
			audioComboBox.setSelectedItem(current.getAudioResource().toString());
			ResourceType type = ResourceType.valueOf(audioComboBox.getSelectedItem().toString());
			getMainFrame().getController(CustomizationController.class).updateAudioPolicy(type);
		}
	}
	
	

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if (e.getSource() == submitBtn) {
			// Get selected child
			ArrayList<Child> children = (ArrayList<Child>) getMainFrame()
					.getController(CustomizationController.class).getChildren();
			Child child = children.get(childComboBox.getSelectedIndex());

			// Get selected Resources
			String imageResource = imageComboBox.getSelectedItem().toString();
			String audioResource = audioComboBox.getSelectedItem().toString();

			// Update selected child with selected resources
			
			
			int currentIndex=children.indexOf(child);
			Child auxChild=children.get(currentIndex);
			auxChild.setImageResource(ResourceType.valueOf(imageResource));
			
			children.remove(currentIndex);
			children.add(currentIndex, auxChild);
			
			getMainFrame().getController(CustomizationController.class)
			.submitUpdate(child, imageResource, audioResource);
			
			
			for (Child aux : children) {
				System.out.println(aux.getName() + " " + aux.getImageResource()
						+ " " + aux.getAudioResource());

			}
			
			return;
		}
		
		// Sample Image clicked: Play audio
		if (e.getSource() instanceof JButton) {
			JButton b = (JButton) e.getSource();
			int number = Integer.parseInt(b.getName());
			getMainFrame().getController(CustomizationController.class)
					.playAudio(number);
		}

	}
}
